package com.sec.etech.ism.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sec.etech.ism.mapper.MaterialNoLogMapper;
import com.sec.etech.ism.model.MaterialNoLog;
import org.openbpm.base.core.id.IdUtil;
import org.openbpm.base.core.util.StringUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
public class MaterialNoLogService extends ServiceImpl<MaterialNoLogMapper, MaterialNoLog> {
    @Resource
    JdbcTemplate jdbcTemplate;

    /**
     * 创建物料号新增/更新记录表
     *
     * @param busId
     * @param busType
     * @param busParams
     * @param materialNo
     * @param num
     * @param name
     */
    public void saveOrUpdateByCreate(Long busId, Integer busType, String busParams, String materialNo,
                                     String num, String name) {
        Date createTime = null;
        Integer result = null;
        if (StringUtil.isNotEmpty(materialNo)) {
            createTime = new Date();
            result = 1;
        } else {
            result = 0;
        }
        this.saveOrUpdateMaterialNoLog(1, busId, busType, busParams, materialNo, result, createTime, num, name);
    }

    /**
     * 更新物料号新增/更新记录表
     *
     * @param busId
     * @param busType
     * @param busParams
     * @param materialNo
     * @param result
     * @param num
     * @param name
     */
    public void saveOrUpdateByUpdate(Long busId, Integer busType, String busParams, String materialNo,
                                     Integer result, String num, String name) {

        this.saveOrUpdateMaterialNoLog(2, busId, busType, busParams, materialNo, result, null, num, name);
    }

    /**
     * 保存或者更新物料号记录表
     *
     * @param busId
     * @param busType
     * @param materialNo
     * @param num
     * @param name
     */
    private void saveOrUpdateMaterialNoLog(Integer type, Long busId, Integer busType, String busParams, String materialNo,
                                           Integer result, Date createTime, String num, String name) {
        MaterialNoLog materialNoLog = this.baseMapper.selectOne(Wrappers.<MaterialNoLog>lambdaQuery()
                .eq(MaterialNoLog::getBusId, busId)
                .eq(MaterialNoLog::getBusType, busType));

        materialNoLog = materialNoLog != null ? materialNoLog : new MaterialNoLog();
        materialNoLog.setType(type);
        materialNoLog.setMaterialNo(materialNo);
        materialNoLog.setResult(result);
        materialNoLog.setCreateTime(createTime);
        materialNoLog.setNum(num);
        materialNoLog.setName(name);
        if (materialNoLog.getId() != null && materialNoLog.getId() > 0) {//更新
            this.baseMapper.updateById(materialNoLog);
        } else {//新增
            materialNoLog.setId(IdUtil.getUId());
            materialNoLog.setBusId(busId);
            materialNoLog.setBusType(busType);
            materialNoLog.setBusParams(busParams);
            this.baseMapper.insert(materialNoLog);
        }
        this.updateBusMaterialNo(busType, materialNo, busId);
    }

    //手动维护物料号
    public void updateMaterialNo(Long logId) {
        MaterialNoLog log = this.baseMapper.selectById(logId);
        this.updateBusMaterialNo(log.getBusType(), log.getMaterialNo(), log.getBusId());
    }

    //更新业务表物料号
    private void updateBusMaterialNo(Integer busType, String materialNo, Long busId) {
        if (StringUtil.isEmpty(materialNo)) {
            return;
        }
        String sql = null;
        if (busType == 1) {
            sql = "update prod_component set component_SAPnum=? where componentid=?";
        } else if (busType == 2) {
            sql = "update prod_parts set parts_SAPnum=? where partsid=?";
        } else if (busType == 3) {
            //毛坯数量为空的默认写1
            String sqlTemp = "select partsid,billet_SAPnum,billet_num from prod_parts where partsid=?";
            Map<String, Object> map = jdbcTemplate.queryForMap(sqlTemp, busId);
            if (map.get("billet_num") == null || "0".equals(map.get("billet_num").toString())) {
                sql = "update prod_parts set billet_SAPnum=?,billet_num=1 where partsid=?";
            } else if (map.get("billet_SAPnum") == null || "".equals(map.get("billet_SAPnum").toString())) {
                sql = "update prod_parts set billet_SAPnum=? where partsid=?";
            }
        } else if (busType == 4) {
            sql = "update procurement_task_sap set SAPnum=? where componentid=?";
        }
        if (sql != null && !"".equals(sql)) {
            jdbcTemplate.update(sql, materialNo, busId);
        }
    }
}
